package logic

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"math/rand"
	"strconv"
	"takeTaxi/common/idl/user"
	"takeTaxi/common/models/mysql"
	"takeTaxi/common/models/redis"
	"takeTaxi/pkg"
	"time"
)

type UserServer struct {
	user.UnimplementedUserServer
}

func (u *UserServer) SendSms(ctx context.Context, in *user.SendSmsReq) (*user.SendSmsResp, error) {
	code := rand.Intn(9000) + 1000
	redis.SetRedis("SendSms"+in.Source+in.Mobile, code)
	fmt.Println(code)
	return &user.SendSmsResp{Greet: "发送成功"}, nil
}

func (u *UserServer) UserRegister(ctx context.Context, in *user.UserRegisterReq) (*user.UserRegisterResp, error) {
	val := redis.GetRedis("SendSms" + "register" + in.Mobile).Val()
	if val != in.Code {
		return nil, errors.New("验证码错误")
	}
	if val == "" {
		return nil, errors.New("验证码失效")
	}
	p := &mysql.Passenger{}
	mobile, err := p.FindByMobile(in.Mobile)
	if err != nil {
		return nil, err
	}
	if mobile.Id != 0 {
		return nil, errors.New("该手机号已注册")
	}
	err = p.Create(in.Mobile)
	if err != nil {
		return nil, errors.New("注册失败")
	}
	return &user.UserRegisterResp{Greet: "注册成功"}, nil
}

func (u *UserServer) UserLogin(ctx context.Context, in *user.UserLoginReq) (*user.UserLoginResp, error) {
	val := redis.GetRedis("SendSms" + "login" + in.Mobile).Val()
	if val != in.Code {
		return nil, errors.New("验证码错误")
	}
	if val == "" {
		return nil, errors.New("验证码失效")
	}
	p := &mysql.Passenger{}
	mobile, err := p.FindByMobile(in.Mobile)
	if err != nil {
		return nil, err
	}
	if mobile.Id == 0 {
		return nil, errors.New("该手机号还未注册")
	}
	claims := pkg.CustomClaims{ID: uint(mobile.Id)}
	token, err := pkg.NewJWT("2211a").CreateToken(claims)
	if err != nil {
		return nil, err
	}

	return &user.UserLoginResp{Greet: token}, nil
}

func (u *UserServer) UpdateUserMessage(ctx context.Context, in *user.UpdateUserMessageReq) (*user.UpdateUserMessageResp, error) {
	p := &mysql.Passenger{}
	err := p.UpdateMobile(in)
	if err != nil {
		return nil, err
	}
	return &user.UpdateUserMessageResp{Greet: "修改失败"}, nil
}

func (u *UserServer) UserRecharge(ctx context.Context, in *user.UserRechargeReq) (*user.UserRechargeResp, error) {
	p := &mysql.Passenger{}
	err := p.UserRecharge(in.Price, in.UserId)
	if err != nil {
		return nil, err
	}
	orderSn := uuid.New().String()
	price := strconv.FormatFloat(float64(in.Price), 'f', 2, 64)
	pkg.AliPay("充值", price, orderSn)
	return &user.UserRechargeResp{Greet: "充值成功"}, nil
}

func (u *UserServer) UserWithdrawal(ctx context.Context, in *user.UserWithdrawalReq) (*user.UserWithdrawalResp, error) {
	p := &mysql.Passenger{}
	err := p.UserWithdrawal(in.Price, in.UserId)
	if err != nil {
		return nil, err
	}
	orderSn := uuid.New().String()
	price := strconv.FormatFloat(float64(in.Price), 'f', 2, 64)
	pkg.AliPay("提现", price, orderSn)
	return &user.UserWithdrawalResp{Greet: "提现成功"}, nil
}

func (u *UserServer) UserTripList(ctx context.Context, in *user.UserTripListReq) (*user.UserTripListResp, error) {
	t := &mysql.Trip{}
	id, err := t.FindByUserId(in.UserId)
	if err != nil {
		return nil, err
	}
	var Items []*user.UserTripList
	for _, m := range id {
		finishedAt := time.Until(m.FinishedAt).String()
		Items = append(Items, &user.UserTripList{
			DriverId:   m.DriverId,
			StarAddr:   m.StarAddr,
			EndAddr:    m.EndAddr,
			FinishedAt: finishedAt,
			Status:     m.Status,
		})
	}
	return &user.UserTripListResp{List: Items}, nil
}
